/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.ims.modules.wechat.dao.WechatMemberDao;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.common.persistence.BaseEntity;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.thinkgem.jeesite.modules.sys.entity.User;

/**
 * 微信会员Service
 * @author Jakemanse
 * @version 2013-09-08
 */
@Component
@Transactional(readOnly = true)
public class WechatMemberService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(WechatMemberService.class);
	
	@Autowired
	private WechatMemberDao wechatMemberDao;
	
	public WechatMember get(Long id) {
		return wechatMemberDao.findOne(id);
	}
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-8  下午11:15:53
	 * @function <p> 根据微信 OpenId 得到 微信会员信息 </p>
	 * @param user
	 * @param wechatId
	 * @return
	 */
	public WechatMember getByWechatMemberId(User user,String wechatId){
		DetachedCriteria dc = wechatMemberDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", user.getId()));
		dc.add(Restrictions.eq("wechatId", wechatId));
		List<WechatMember> members = wechatMemberDao.find(dc);
		if(members!=null && members.size()>0){
			return members.get(0);
		}
		return null;
	}
	
	
	
	
	public WechatMember getByMobile(User user,String mobile){
		DetachedCriteria dc = wechatMemberDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", user.getId()));
		dc.add(Restrictions.eq("mobile", mobile));
		List<WechatMember> members = wechatMemberDao.find(dc);
		if(members!=null && members.size()>0){
			return members.get(0);
		}
		return null;
	}
	
	
	
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-23  上午11:25:57
	 * @function <p> 根据微信 绑写的手机号 得到 微信会员信息 </p>
	 * @param user
	 * @param wechatId
	 * @return
	 */
	public WechatMember getByWechatMobile(User user,String mobile){
		DetachedCriteria dc = wechatMemberDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", user.getId()));
		dc.add(Restrictions.eq("mobile", mobile));
		List<WechatMember> members = wechatMemberDao.find(dc);
		if(members!=null && members.size()>0){
			return members.get(0);
		}
		return null;
	}
	
	public Page<WechatMember> find(Page<WechatMember> page, WechatMember wechatMember) {
		DetachedCriteria dc = wechatMemberDao.createDetachedCriteria();
		if (wechatMember.getUser()!=null && wechatMember.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", wechatMember.getUser().getId()));
		}
		if (StringUtils.isNotEmpty(wechatMember.getName())){
			dc.add(Restrictions.like("name", "%"+wechatMember.getName()+"%"));
		}
		
		if(wechatMember.getCreateTime() !=null){
			String timeField = "createTime";
			dc.add(Restrictions.ge(timeField,wechatMember.getCreateTime() ));
		}
		
		//大于等的分数
		if (wechatMember.getScore()!=null ){
			dc.add(Restrictions.ge("score", wechatMember.getScore()));
		}		
		
		if(wechatMember.getEndTime() !=null){
			String timeField = "createTime";
			dc.add(Restrictions.le(timeField,wechatMember.getEndTime()));
		}
		
		if (StringUtils.isNotEmpty(wechatMember.getMobile())){
			dc.add(Restrictions.like("mobile", "%"+wechatMember.getMobile()+"%"));
		}
		if (StringUtils.isNotEmpty(wechatMember.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+wechatMember.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", WechatMember.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return wechatMemberDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(WechatMember wechatMember) {
		wechatMemberDao.save(wechatMember);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		wechatMemberDao.deleteById(id);
	}
	
	@Transactional(readOnly = false)
	public void deleteByUserIdAndWechatId(Long userId,String wid) {
		wechatMemberDao.deleteByUserIdAndWechatId(userId,wid);
	}
	
	
	
}
